package com.cykj.web.controller.posbiz.entry.controller;

import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.cykj.common.core.domain.entity.SysUser;
import com.cykj.common.utils.SecurityUtils;
import com.cykj.pos.domain.BizPosErminalRate;
import com.cykj.pos.mapper.BizPosMachineMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cykj.common.annotation.Log;
import com.cykj.common.core.controller.BaseController;
import com.cykj.common.core.domain.AjaxResult;
import com.cykj.common.enums.BusinessType;
import com.cykj.web.controller.posbiz.entry.domain.BizMerchantEntryNewDTO;
import com.cykj.web.controller.posbiz.entry.service.IBizMerchantEntryNewService;
import com.cykj.common.utils.poi.ExcelUtil;
import com.cykj.common.core.page.TableDataInfo;

/**
 * 商户入件Controller
 *
 * @author zhj
 * @date 2021-11-03
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/pos/entryNew" )
public class BizMerchantEntryNewController extends BaseController {

    private final IBizMerchantEntryNewService iBizMerchantEntryNewService;

    @Autowired
    BizPosMachineMapper posMachineMapper;

    /**
     * 查询商户入件列表  解绑
     */
    @GetMapping("/list")
    public TableDataInfo list(BizMerchantEntryNewDTO bizMerchantEntry) {
        SysUser sysUserman = SecurityUtils.getLoginUser().getUser();
        if (sysUserman.getUserId()==1){
            sysUserman.setUserId(2L);
        }
        bizMerchantEntry.setMerchIdPros(sysUserman.getUserId());
        Long merchId= posMachineMapper.setMerchId(sysUserman.getUserId());
        bizMerchantEntry.setMerchId(merchId);
        startPage();
        List<BizMerchantEntryNewDTO> list = iBizMerchantEntryNewService.queryList(bizMerchantEntry);
        return getDataTable(list);
    }
    /**
     * @author Wangjiaqi
     * @date 2022/02/26
     *
     * sn设备列表
     */
    @GetMapping("/selUnbindlist")
    public AjaxResult selUnbindlist(BizMerchantEntryNewDTO bizMerchantEntry) {
        List<BizMerchantEntryNewDTO> list = iBizMerchantEntryNewService.selUnbindlist(bizMerchantEntry);
        return AjaxResult.success(list);
    }
    /**
     * 查询商户入件列表  换绑
     */
    @GetMapping("/list/inTie")
    public TableDataInfo inTieList(BizMerchantEntryNewDTO bizMerchantEntry) {
        startPage();
        List<BizMerchantEntryNewDTO> list = iBizMerchantEntryNewService.inTieList(bizMerchantEntry);
        return getDataTable(list);
    }

    /**
     * 导出商户入件列表
     */
    @PreAuthorize("@ss.hasPermi('pos:entry:export')" )
    @Log(title = "商户入件" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(BizMerchantEntryNewDTO bizMerchantEntry) {
        List<BizMerchantEntryNewDTO> list = iBizMerchantEntryNewService.queryList(bizMerchantEntry);
        ExcelUtil<BizMerchantEntryNewDTO> util = new ExcelUtil<BizMerchantEntryNewDTO>(BizMerchantEntryNewDTO.class);
        return util.exportExcel(list, "entry" );
    }

    /**
     * 获取商户入件详细信息
     */
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iBizMerchantEntryNewService.getMerchantEntryId(id));
    }

    @GetMapping(value = "selfees/{sn}" )
    public AjaxResult selfees(@PathVariable("sn" ) String sn) {
        return AjaxResult.success(iBizMerchantEntryNewService.getMerchantEntryselSeesSn(sn));
    }

    /**
     * 新增商户入件
     */
    @PreAuthorize("@ss.hasPermi('pos:entry:add')" )
    @Log(title = "商户入件" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizMerchantEntryNewDTO bizMerchantEntry) {
        return toAjax(iBizMerchantEntryNewService.save(bizMerchantEntry) ? 1 : 0);
    }

    /**
     * 修改商户入件
     */
    @PreAuthorize("@ss.hasPermi('pos:entry:edit')" )
    @Log(title = "商户入件" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizMerchantEntryNewDTO bizMerchantEntry) {
        return toAjax(iBizMerchantEntryNewService.updateByMerchantEntryId(bizMerchantEntry));
    }

    /**
     * 删除商户入件
     */
    @PreAuthorize("@ss.hasPermi('pos:entry:remove')" )
    @Log(title = "商户入件" , businessType = BusinessType.DELETE)
    @GetMapping("/delEntryjb/{id}" )
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(iBizMerchantEntryNewService.removeMerchantEntryIds(id));
    }
    @GetMapping("/sh/{type}" )
    public AjaxResult selectsh(@PathVariable String type) {
        return AjaxResult.success(iBizMerchantEntryNewService.selectsh(type));
    }
    @GetMapping("/si/{item}" )
    public AjaxResult selectsi(@PathVariable String item) {
        return AjaxResult.success(iBizMerchantEntryNewService.selectsi(item));
    }
    @GetMapping("/q/{item}" )
    public AjaxResult selectq(@PathVariable String item) {
        return AjaxResult.success(iBizMerchantEntryNewService.selectq(item));
    }
    @GetMapping("/selfeeChl" )
    public AjaxResult selfeechl() {
        return AjaxResult.success(iBizMerchantEntryNewService.selfeechl());
    }

    /**
     * 查询商户入件列表  补件
     */
    @PostMapping("/patch")
    public AjaxResult patch(BizMerchantEntryNewDTO bizMerchantEntry) {
        /*修改报件表*/
        int row1=iBizMerchantEntryNewService.updateBizMerchantEntry(bizMerchantEntry);
        /*查询*/
        BizMerchantEntryNewDTO bizMerchantEntryNewDTOS = iBizMerchantEntryNewService.selectEntryAlls(bizMerchantEntry.getId());
        /*插入到报件关联的BizEntrySn表*/
        int row2=iBizMerchantEntryNewService.intBizEntrySn(bizMerchantEntryNewDTOS);
        /*添加激活表*/
        int row3=iBizMerchantEntryNewService.intStatusRecords(bizMerchantEntryNewDTOS);
        /*修改设备表状态*/
        int row4=iBizMerchantEntryNewService.updatePosMachine(bizMerchantEntryNewDTOS);
        posMachineMapper.generateAllPosMerchantRelated();
        if (row1+row2+row3+row4>3){
            return AjaxResult.success("补件成功");
        }else {
            return AjaxResult.error("补件失败");
        }
    }
}
